package com.woyou.ui.activity.orderlist;

import java.util.ArrayList;
import java.util.List;

import retrofit.RetrofitError;
import android.content.Context;
import android.text.TextUtils;

import com.woyou.bean.Info;
import com.woyou.bean.MyOrderItem;
import com.woyou.bean.OrderDetail;
import com.woyou.bean.Result;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.OrderDefaultReq;
import com.woyou.controller.SuperController;
import com.woyou.service.NetWorkCenter;
import com.woyou.ui.activity.HomeActivity_;
import com.woyou.ui.activity.LoginActivity_;
import com.woyou.ui.activity.PayOnlineActivity;
import com.woyou.ui.fragment.IndexFragment_;
import com.woyou.ui.fragment.MyFragment_;
import com.woyou.ui.view.SuperUI;
import com.woyou.utils.DateUtils;
import com.woyou.utils.Dialog;

public class MyOrderActController extends SuperController{
	
	private MyOrderActivity mView;
	
	private Dialog dialog = new Dialog();
	private OrderDefaultReq orderDefaultReq = new OrderDefaultReq();
	
	public MyOrderActController(Context context, SuperUI superUI) {
		super(context, superUI);
		mView = (MyOrderActivity) superUI;
		
	}
	
	@Override
	public boolean backOperate() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * 加载订单详情
	 * 
	 * @param oId
	 */
	public void loadOrderDetail(final String oId) {
		if (!NetWorkCenter.isNetworkConnected(mContext)) {
			showToast("请连接网络");
			return;
		}

		executeTask(new Runnable() {
			
			@Override
			public void run() {
				try {
					UserInfo info = mUserModel.getUserInfo();
					if (info != null && !info.getuId().equals("")) {
						orderDefaultReq.setoId(oId);
						orderDefaultReq.setuId(info.getuId());
						orderDefaultReq.setPwd(info.getPwd());
						final Result<OrderDetail> result = mOrderModel.v2_3queryOrderDetail(orderDefaultReq);
						if (result != null && result.getCode() == 1) {
							OrderDetail mOrderDetail = result.getData();
							Info<OrderDetail> inf = new Info<OrderDetail>(mOrderDetail);
							inf.setOrigin(MyOrderActivity.class);
							openActivity(PayOnlineActivity.class, inf);
							mView.finish();
							
						} else if (result != null && result.getCode() == -3) {
							runOnUI(new Runnable() {
								@Override
								public void run() {
									dialog.LoginToast(mContext, result.getMsg(), MyOrderActivity.class);
								}
							});
							
						} 
					} else {
						showToastOnUI("登录后方可查看订单详情");
						openActivity(LoginActivity_.class, null);
						
					}
				} catch (RetrofitError e) {
					e.printStackTrace();
				} 
			}
		});
	}
	
	
	/**
	 * 添加订单分隔线的bean
	 */
	public List<MyOrderItem> addOrderSeparator(List<MyOrderItem> myOrders) {
		if (myOrders.size() == 0)
			return new ArrayList<MyOrderItem>();

		// 遍历所有订单，给每个订单分出日期和时间
		recomData(myOrders);

		// 添加第一个等待订单
		addWaitOrder(myOrders);

		for (int i = 1; i < myOrders.size(); i++) { // 从第二个订单开始,(目前不可能同时有2张等待订单)
			MyOrderItem o1 = myOrders.get(i);
			if (o1.Type == 0)
				continue;
			if (i + 1 < myOrders.size()) {
				MyOrderItem o2 = myOrders.get(i + 1);
				if (o2.Type == 0)
					continue;
				if (!TextUtils.equals(o1.oDate, o2.oDate)) {
					MyOrderItem o3 = new MyOrderItem();
					o3.Type = 0;
					o3.oDate = o2.oDate;
					o3.setsName(o2.getsName());
					o3.setState(o2.getState());
					o3.setoTimeStamp(o2.getoTimeStamp());
					myOrders.add(i + 1, o3);
				}

			}

		}
		return myOrders;

	}

	/**添加等待订单*/
	private void addWaitOrder(List<MyOrderItem> myOrders) {
		MyOrderItem order = myOrders.get(0); // 第一个分割线
		if (order.Type == 1) {
			if (2 == order.getState()) { // 等待订单(即推送成功，state=2)
				order.Type = 2;
				
				if(myOrders.size()<2)
					return;
				MyOrderItem or = new MyOrderItem(); // 在等待订单下面增加一个分割线
				MyOrderItem orderItem = myOrders.get(1);
				or.Type = 0;
				or.oDate = orderItem.oDate;
				or.setsName(orderItem.getsName());
				or.setState(orderItem.getState());
				or.setoTimeStamp(orderItem.getoTimeStamp());
				myOrders.add(1, or);
			} else {
				MyOrderItem or = new MyOrderItem();
				or.Type = 0;
				or.oDate = order.oDate;
				or.setsName(order.getsName());
				or.setState(order.getState());
				or.setoTimeStamp(order.getoTimeStamp());
				myOrders.add(0, or);
			}
			
		}
	}
	
	/**重组数据*/
	private void recomData(List<MyOrderItem> myOrders) {
		MyOrderItem item = null;
		for (int i = 0; i < myOrders.size(); i++) {
			item = myOrders.get(i);
			long timeStamp = item.getoTimeStamp();
			String formatDate = DateUtils.formatDate(timeStamp);
			item.setoDate(formatDate);
			String time = DateUtils.getTime(timeStamp);
			item.setoTime(time);
			item = null;
		}
	}
	
	@SuppressWarnings("rawtypes")
	public void exitActivity(String originClassName) {
		// 退出我的订单界面
		if(originClassName!=null && originClassName.equals(IndexFragment_.class.getName())){
			Info<Class> info = new Info<Class>();
			info.setData(IndexFragment_.class);
			openActivity(HomeActivity_.class, info);
			mView.finish();
			
		} else {
			Info<Class> info = new Info<Class>();
			info.setData(MyFragment_.class);
			openActivity(HomeActivity_.class, info);
			mView.finish();
		}
		
	}
	
}
